from rest_framework.views import exception_handler
from .response import APIResponse
from .logger import logger


def common_exception_handler(exc, context):
    ret = exception_handler(exc, context)
    logger.error("view是：%s 出现了错误：%s" % (context['view'].__class__.__name__, str(exc)))
    if not ret:
        if isinstance(exc, KeyError):
            return APIResponse(code=0, msg='key error')
        return APIResponse(code=0, msg='error', result=str(exc))
    else:
        return APIResponse(code=0, msg='error', result=ret.data)
